﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Composite.Functions;
using Composite.Data;
using Composite.Data.Types;
using System.Text;
using LCA.Utils;

public partial class C1Function : Composite.AspNet.UserControlFunction
{
    public override string FunctionDescription
    {
        get
        {
            return "Flow details";
        }
    }


    private string FlowId
    {
        get
        {
            string path = Composite.Core.Routing.Pages.C1PageRoute.GetPathInfo();
            //string path = HttpContext.Current.Request.Url.PathAndQuery;

            if (path == null)
                return null;
            else
                return path.Trim().Replace("/", "");
            
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Composite.Core.Routing.Pages.C1PageRoute.RegisterPathInfoUsage();
        if (!IsPostBack)
        {
            RefreshComponents();
        }
    }

    private void RefreshComponents()
    {

        if (FlowId == null)
        {
            plhFlowInfo.Visible = true;
            litInfo.Text = "Please select node on the left to see flow details";
            return;
        }

        int result;
        if (string.IsNullOrEmpty(FlowId) || !int.TryParse(FlowId, out result))
        {
            plhErrorMessage.Visible = true;
            litTopErrorMessage.Text = "Flow is not found.";
            return;
        }

        try
        {
            using (DataConnection conn = new DataConnection())
            {
                var flow = (from f in conn.Get<LCA.Data.Flow>()
                           join u in conn.Get<LCA.Data.Unit>() on f.Unit equals u.Id into joined
                           from j in joined.DefaultIfEmpty()
                           where f.IsDeleted == false && f.FlowId == int.Parse(FlowId)
                           select new
                           {
                               Id = f.Id,
                               Name = f.Name,
                               Description = f.Description,
                               Source = f.Source,
                               UnitName = j.Name,
                               PicturePath = f.Picture,
                               ChangeDate = f.ChangeDate,
                               CreatedDate = f.CreatedDate
                           }).FirstOrDefault();


                if (flow == null)
                {
                    plhErrorMessage.Visible = true;
                    litTopErrorMessage.Text = "Flow is not found.";
                    return;
                }

                plhFlowDetails.Visible = true;
                litTitle.Text = flow.Name;
                litDescription.Text = flow.Description;
                litUnit.Text = string.IsNullOrEmpty(flow.UnitName) ? "-" : flow.UnitName;
                litSource.Text = flow.Source;
                litImg.Text = string.IsNullOrEmpty(flow.PicturePath) ? "" : string.Format("<img src='{0}'/>", UrlHelper.GetMediaUrl(flow.PicturePath) );
                litCreationDate.Text = string.Format("{0:dd-MMM-yyyy}", flow.CreatedDate); 
                litModifiedDate.Text = string.Format("{0:dd-MMM-yyyy}", flow.ChangeDate);


                //var path = UrlHelper.GetMediaUrl(flow.PicturePath);
                //if(path == null)
                //{
                //    lnkProductionDiagram.CssClass += " disabled";
                //}
                //else
                //{
                //    lnkProductionDiagram.NavigateUrl = path;
                //}
                

                // Focus at selected tree node.
                string script = @" $(document).ready(function () { clickFlow('" + flow.Id + "'); });";
                Page.ClientScript.RegisterStartupScript(GetType(), ClientID + "_TriggerClickFlow", script, true);
            }
        }
        catch (Exception ex)
        {
            plhErrorMessage.Visible = true;
            litTopErrorMessage.Text = string.Format("Problem occured.</br>Details: {0}", ex.Message);
            plhFlowDetails.Visible = false;
            return;
        }
    }
}